<template>
  <div>
    购物车
  </div>
</template>
 
<script setup lang='ts'>
import type { goodsI } from './interface/index'
import { aGoods } from './data/data'

class UIGoods {
  goods: any
  choose!: number
  totalPrice!: number

  constructor(goods: goodsI, choose: number = 0, totalPrice: number = 0) {
    const g = { ...goods }
    Object.freeze(g)
    // 商品
    Object.defineProperty(this, 'goods', {
      get() {
        return goods
      },
      set() {
        throw new Error("'goods'属性不可赋值")
      },
      configurable: false,  // 不可在配置属性描述符
    })

    // 选择数量
    Object.defineProperty(this, 'choose', {
      configurable: false,
      get() {
        return choose
      },
      set(val: number) {
        if (typeof val !== 'number') {
          throw new Error('choose类型为正整数')
        }
        if (val !== parseInt(`${val}`)) {
          throw new Error('choose类型为正整数')
        }
        if (val < 0) {
          throw new Error('choose类型为正整数')
        }
        choose = val
      }

    })

    // 总价
    Object.defineProperty(this, 'totalPrice', {
      configurable: false,
      get() {
        totalPrice = this.goods.price * this.choose
        return totalPrice
      },
    })

    Object.seal(this)
  }

}

const g = new UIGoods(aGoods)
g.choose = 3
console.log(g.goods)

</script>
 
<style lang='scss' scoped>

</style>